<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('文章类别修改')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item" th:if="${model.id != null}">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input id="txtid" autocomplete="off" class="layui-input" lay-verify="required" readonly
                               name="id"
                               th:value="${model.id}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级</label>
                    <div class="layui-input-block">
                        <ul class="dtree" data-id="-1" id="selectParent" name="parentId"
                            th:attr="data-value=${model.parentId}"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required|title" placeholder="请输入标题"
                               class="layui-input"
                               th:value="${model.title}"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" lay-verif="sort" class="layui-input" th:value="${model.sort}"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="class-save" lay-submit=""
                        type="submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button class="pear-btn pear-btn-sm" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    let addUrl = '[(${@htmlHelper.actionGuid("add")})]';
    let editUrl = '[(${@htmlHelper.actionGuid("edit")})]';
    let parentComboUrl = '[(${@htmlHelper.actionGuid("parentCombo")})]';
    let viewType = "add";
    // if ($("#txtid").val() > 0) {
    //     viewType = 'edit';
    // }
</script>
<script>
    layui.use(['form', 'dtree'], function () {
        let MODULE_PATH = contextPath + "/article/class/";
        let form = layui.form;
        let dtree = layui.dtree;

        /**
         * 初始化 父级 的下拉菜单项
         */
        let dtreeObj = dtree.renderSelect({
            elem: "#selectParent",
            //url: MODULE_PATH + "selectParent",
            url: createLink(parentComboUrl),
            method: 'post',
            line: true, // 有线树
            ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
            icon: ["0", "2"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
            selectInputName: {nodeId: "parentId", context: "title"},//selectParent_select_input  selectParent_select_nodeId
            //skin: "layui",
            //dataFormat: "tree",
            dataStyle: "layuiStyle",
            //record: true,//开启参数里面增加 recordData，这里存放的是当前数据库的数据
            //response: {treeId: "id", parentId: "parentId", title: "title",rootName: "data"},  //修改response中返回数据的定义
            selectInitVal: "[[${model.parentId}]]",//注意这边需要是字符串，不然默认初始值不会赋值
            response: {
                statusName: "code", //返回标识（必填）
                statusCode: 0, //返回码（必填）
                message: "msg", //返回信息（必填）
                rootName: "data", //根节点名称（必填）
                treeId: "id", //节点ID（必填）
                //parentId: "parentId", //父节点ID（必填）
                title: "text", //节点名称（必填）
            },
            //selectInitVal:"0",
            //selectInitVal: "[[${model.parentId}]]"//注意这边需要是字符串，不然默认初始值不会赋值
            // 树加载完成后调用
            success: function (data, $ul, first) {
                if (data != null && data.length > 0) {
                    //let tmpData = [{ "value": -1, "name": '请选择', "children": result.data }];
                    //return { 'code': 0, 'msg': '', 'data': tmpData }; // dataStyle: "layuiStyle" 返回的格式为这个
                    //let tmpData = { "value": -1, "name": '请选择' };
                    //result.data.unshift(tmpData);
                    return {'code': 0, 'msg': '', 'data': data}; // dataStyle: "layuiStyle" 返回的格式为这个
                }
            },
            done: function (res, $ul, first) {
                if (first) {
                    //var params = dtree.dataInit("selectParent", "3");
                }
            }
        });

        form.on('submit(class-save)', function (data) {
            /**
             * data 包含 icon,#select的id_select_input
             * title(selectInputName: {nodeId: "parentId", context: "title"}这里的title)
             * 以及form里各个输入框 name 的
             */
            let _type = 'post';
            if(data.field.id){_type= 'put';}
            data.field.selectParent_select_input = undefined;
            $.ajax({
                //url: MODULE_PATH + 'update',
                url: createLink(ActionGUID),
                //data: JSON.stringify(data.field),
                data: data.field,
                //dataType: 'json',
                //contentType: 'application/json',
                type: _type,
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("class-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //初始化下拉
        //dtreeObj.reload();
        //dtreeObj.loadTreeInit();
        //dtreeObj.selectVal("3111");
        // $("#selectParent").val();
        // form.render('select');
        // dtreeObj.selectVal('[[${model.parentId}]]');
        // dtreeObj.dataInit('[[${model.parentId}]]');
        // dtreeObj.refreshTree();
    });


</script>
</body>
</html>